package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class DetailPage extends Utils {
    private static String URL = detailUrl;
    public DetailPage() {
        super(URL);
    }
    public void checkStaticDetail() throws InterruptedException, IOException {
        Thread.sleep(2000);
        //查看元素是否存在
        //标题
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title"));
        //日期
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.date"));
        //内容 - 因为文本内容涉及多个类型的标签，但都在同一个div中，因此通过父标签进行判断
        driver.findElement(By.cssSelector("#detail"));
        //编辑
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)"));
        //删除
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)"));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    public void checkEdit() throws IOException, InterruptedException {
        //生成动态标题
        SimpleDateFormat sim = new SimpleDateFormat("HH-mm-ss");
        String titleTime = sim.format(System.currentTimeMillis());
        //测试编辑功能
        String titleBefore = driver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
//        System.out.println(titleBefore);
        //编辑标题
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(1)")).click();
        Thread.sleep(500);
        driver.findElement(By.cssSelector("#title")).clear();
        WebElement clickable = driver.findElement(By.cssSelector("#title"));
        //双击鼠标左键doubleClick
        new Actions(driver).doubleClick(clickable).sendKeys("修改后的标题" + titleTime).perform();
        Thread.sleep(500);
        //编辑内容
        clickable = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span"));
        //单机鼠标左键click
        new Actions(driver).click(clickable).sendKeys("修改后的内容" + titleTime).perform();
        //提交内容
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(1500);
        String titleAfter = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(2) > div.title")).getText();
//        System.out.println(titleAfter);
        assert !titleBefore.equals(titleAfter);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    public void checkDel() throws IOException, InterruptedException {
        //进入编辑页
        driver.get(URL);
        Thread.sleep(500);
        //测试删除功能
        //点击删除
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        //切换到弹窗
        Alert alert = driver.switchTo().alert();
        //取消
        alert.dismiss();
        //点击删除
        driver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();
        //确认
        alert.accept();
        Thread.sleep(1000);
        assert driver.getCurrentUrl().equals("http://8.137.19.140:9090/blog_list.html");
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
}
